package com.gitee.mocoffee.flow;

import java.util.concurrent.ConcurrentHashMap;

import lombok.extern.slf4j.Slf4j;

/**
 * 业务异常处理器,分别对应正向和逆向场景中的异常情况
 *
 * @author lijia
 */
public interface ProcessExceptionHandler<INPUT> {
	void onProcessException(Processor<INPUT> processor, INPUT input, ConcurrentHashMap<String, Object> context,
			Exception ex);

	void onRollbackException(Processor<INPUT> processor, INPUT input, ConcurrentHashMap<String, Object> context,
			Exception ex);

	/**
	 * 默认的业务异常处理器,不处理仅打印日志
	 *
	 * @param <T>
	 */
	@Slf4j
	class DefaultProcessExceptionHandler<T> implements ProcessExceptionHandler<T> {
		@SuppressWarnings("rawtypes")
		public static final DefaultProcessExceptionHandler me = new DefaultProcessExceptionHandler();

		private DefaultProcessExceptionHandler() {
		}

		@Override
		public void onProcessException(Processor<T> processor, T t, ConcurrentHashMap<String, Object> context,
				Exception ex) {
			log.error("error at process. processor:" + processor + ", input:" + t + ", context:" + context + "", ex);
		}

		@Override
		public void onRollbackException(Processor<T> processor, T t, ConcurrentHashMap<String, Object> context,
				Exception ex) {
			log.error("error at rollback. processor:" + processor + ", input:" + t + ", context:" + context + "", ex);
		}
	}
}
